package com.jic.member.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.request.ChildNameRequest;
import com.jic.member.entity.MememberGrowthBase;
import com.jic.member.mapper.MemMemberGrowthBaseMapper;
import com.jic.member.request.MemberGrowthBaseRequest;
import com.jic.member.response.MememberGrowthBaseResponse;
import com.jic.member.service.MememberGrowthBaseService;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员等级基础表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-08 11:35:06
 */
@Service
public class MememberGrowthBaseServiceImpl implements MememberGrowthBaseService {
    @Autowired
    private SysDeptApi sysDeptApi;
    @Resource
    private MemMemberGrowthBaseMapper mememberGrowthBaseMapper;


    @Override
    public long insert(MememberGrowthBase mememberGrowthBase) {
        return mememberGrowthBaseMapper.insert(mememberGrowthBase);
    }

    @Override
    public long delete(Long id) {
        return mememberGrowthBaseMapper.delete(id);
    }

    @Override
    public RestResult<Long> update(MemberGrowthBaseRequest memberGrowthBaseRequest) {
        RestResult result = new RestResult();
        MememberGrowthBase mememberGrowthBase = new MememberGrowthBase();
        BeanUtils.copyProperties(memberGrowthBaseRequest, mememberGrowthBase);
        long flag = 0;
        Long deptId = memberGrowthBaseRequest.getAdminOperate().getDeptId();
        //先判断账号所属平台
        Integer deptType = memberGrowthBaseRequest.getAdminOperate().getDeptType().getCode();
        mememberGrowthBase.setDeptType(deptType);
        /*如果是母平台则直接操作*/
        long count1 = 0;
        if (deptType == 0) {
            count1 = mememberGrowthBaseMapper.queryCountByDeptId(deptId);
             if(count1 != 0){
                mememberGrowthBase.setUpdaterName(memberGrowthBaseRequest.getAdminOperate().getOperateName());
                mememberGrowthBase.setUpdateTime(new Date());
                mememberGrowthBase.setDeptId(Math.toIntExact(memberGrowthBaseRequest.getAdminOperate().getDeptId()));
                flag = mememberGrowthBaseMapper.update(mememberGrowthBase);
             }else {
                 mememberGrowthBase.setCreatorName(memberGrowthBaseRequest.getAdminOperate().getOperateName());
                 mememberGrowthBase.setCreateTime(new Date());
                 mememberGrowthBase.setDeptId(Math.toIntExact(memberGrowthBaseRequest.getAdminOperate().getDeptId()));
                 flag = mememberGrowthBaseMapper.insert(mememberGrowthBase);
             }
        }
        result.setCode("0000");
        result.setData(flag);
        return result;
    }

    @Override
    public MememberGrowthBase queryByPrimaryKey(Long id) {
        return mememberGrowthBaseMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberGrowthBase> query(MememberGrowthBase mememberGrowthBase) {
        return mememberGrowthBaseMapper.query(mememberGrowthBase);
    }

    @Override
    public PageResult<MememberGrowthBase> queryPage(MememberGrowthBase mememberGrowthBase, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MememberGrowthBase> list = mememberGrowthBaseMapper.queryPage(startIndex, pageQuery.getLimit(), mememberGrowthBase);
        long count = mememberGrowthBaseMapper.queryPageCount(mememberGrowthBase);
        PageResult pageResult = new PageResult();
        list.forEach(item->{
            RestResult<String> name = sysDeptApi.queryDeptNameById(item.getDeptId().longValue());
            item.setDeptName(name.getData());
        });
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<PageResult<MememberGrowthBaseResponse>>  getGrowthInfo(MemberGrowthBaseRequest memberGrowthBaseRequest) {
        RestResult result = new RestResult();
        PageResult pageResult = new PageResult();
        int startIndex = (memberGrowthBaseRequest.getStart()-1) * memberGrowthBaseRequest.getLimit();
        memberGrowthBaseRequest.setDeptId(memberGrowthBaseRequest.getAdminOperate().getDeptId());
        List<MememberGrowthBaseResponse> list=  mememberGrowthBaseMapper.getGrowthInfo(startIndex,memberGrowthBaseRequest.getLimit(),memberGrowthBaseRequest);
        pageResult.setRows(list);
        result.setData(pageResult);
        result.setCode("0000");
        return  result;

    }

    @Override
    public MememberGrowthBaseResponse getDeptType(MemberGrowthBaseRequest memberGrowthBaseRequest) {
        MememberGrowthBaseResponse mememberGrowthBaseResponse = new MememberGrowthBaseResponse();
        mememberGrowthBaseResponse.setDeptType(memberGrowthBaseRequest.getAdminOperate().getDeptType().getCode());
        long deptId = memberGrowthBaseRequest.getAdminOperate().getDeptId();
        mememberGrowthBaseResponse.setDeptId((int)(deptId));
        return  mememberGrowthBaseResponse;
    }

    @Override
    public RestResult<PageResult<MememberGrowthBaseResponse>> queryBaseRule(MemberGrowthBaseRequest memberGrowthBaseRequest) {
        if(memberGrowthBaseRequest.getMsName() !=null) {
            List<Long> list = sysDeptApi.querymMerchantOrStoreListByName(memberGrowthBaseRequest.getMsName());
            memberGrowthBaseRequest.setDeptIds(list);
        }
         /*先判断当前登录账号*/
        Integer deptType = memberGrowthBaseRequest.getAdminOperate().getDeptType().getCode();
        if(deptType == 0){
            memberGrowthBaseRequest.setDeptType(6);
            memberGrowthBaseRequest.setPlatformId(memberGrowthBaseRequest.getChildId());

        }else if(deptType == 1){
            memberGrowthBaseRequest.setChildId(Math.toIntExact(memberGrowthBaseRequest.getAdminOperate().getBusinessId()));
            memberGrowthBaseRequest.setDeptType(1);
        }
        // 计算下标
        int startIndex = (memberGrowthBaseRequest.getStart() - 1) * memberGrowthBaseRequest.getLimit();
        List<MememberGrowthBaseResponse> list = mememberGrowthBaseMapper.queryBaseRule(startIndex, memberGrowthBaseRequest.getLimit(), memberGrowthBaseRequest);
        long count = mememberGrowthBaseMapper.queryBaseRuleCount(memberGrowthBaseRequest);
        list.forEach(items -> {
            RestResult<String> name = sysDeptApi.queryDeptNameById(Long.valueOf(items.getDeptId()));
            ChildNameRequest request = new ChildNameRequest();
            request.setDeptId(Long.valueOf(items.getDeptId()));
            RestResult<String> names = sysDeptApi.queryChildNameByDeptId(request);
            items.setDeptName(name.getData());
            items.setChildName(names.getData());
        });

        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setRows(list);
        RestResult result = new RestResult();
        result.setData(pageResult);
        result.setCode("0000");
        return result;

    }
}